import { Stages } from "mmb";
import { getEnvStage } from "../../store/helper";
import { encode, decode } from "../../utils/jwt"

import Router from "koa-router";
const router = new Router();

router.post("/_cmd/token/encode", async (ctx, next) => {
  const {userId, userOpenId} = ctx.request.body
  if (getEnvStage() === Stages.PROD) {
    ctx.body = "forbid print token";
  } else {
    ctx.body = await encode({userId, userOpenId}, {expiresIn: `365d`});
  }
});

router.post("/_cmd/token/decode", async (ctx, next) => {
  const {token} = ctx.request.body
  if (getEnvStage() === Stages.PROD) {
    ctx.body = "forbid print token";
  } else {
    ctx.body = await decode(token);
  }
});

export default router;
